package com.zjdiepu.www.biz.system;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.system.BaseCustomerMapper;
import com.zjdiepu.www.ibiz.system.IBaseCustomerService;
import com.zjdiepu.www.model.system.dto.BaseCustomer;
import com.zjdiepu.www.model.system.ext.BaseCustomerExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * 客户Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月24日 14时28分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseCustomerServiceImpl extends BaseService implements IBaseCustomerService {
	public static final Logger logger = LoggerFactory.getLogger(BaseCustomerServiceImpl.class);

	@Autowired
	private BaseCustomerMapper baseCustomerMapper;
	
	@Override
	public List<YwBusiness> selectXhhMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return baseCustomerMapper.selectXhhMultiByExt(ywBusinessQueryRequest);
	}
	
	@Override
	public int countXhhMultiByExt(YwBusinessQueryRequest ywBusinessQueryRequest) {
		return baseCustomerMapper.countXhhMultiByExt(ywBusinessQueryRequest);
	}
	
	@Override
	public List<YwBusiness> selectMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return baseCustomerMapper.selectMultiByExt(ywBusinessQueryRequest);
	}
	
	@Override
	public int countMultiByExt(YwBusinessQueryRequest ywBusinessQueryRequest) {
		return baseCustomerMapper.countMultiByExt(ywBusinessQueryRequest);
	}
	
	@Override
	public void saveUpdateAndDelete(BaseCustomer baseCustomer, OperateType operType) {
		switch (operType) {
		case add:
			baseCustomer.initInsertData(baseCustomer.getModifier(), baseCustomer.getModifierName());
			baseCustomerMapper.insert(baseCustomer);
			break;
		case edit:
			baseCustomer.initUpdateData(baseCustomer.getModifier(), baseCustomer.getModifierName());
			// 影响的记录数
			int updateCount = baseCustomerMapper.updateByPrimaryKeySelective(baseCustomer);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseCustomer.initDeleteData(baseCustomer.getModifier(), baseCustomer.getModifierName());
			// 影响的记录数
			int deleteCount = baseCustomerMapper.updateByPrimaryKeySelective(baseCustomer);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseCustomer> selectByExt(BaseCustomerExt baseCustomerExt) {
		return baseCustomerMapper.selectByExt(baseCustomerExt);
	}
	
	@Override
	public int countByExt(BaseCustomerExt baseCustomerExt){
		return baseCustomerMapper.countByExt(baseCustomerExt);
	}
	
	@Override
	public BaseCustomer selectById(String id) {
		return baseCustomerMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseCustomerMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseCustomer baseCustomer) {
		return baseCustomerMapper.countRefined(baseCustomer);
	}

	@Override
	public BaseCustomer selectRefined(BaseCustomer baseCustomer) {
		return baseCustomerMapper.selectRefined(baseCustomer);
	}

	@Override
	public boolean repeat(BaseCustomer baseCustomer) {
		int count = countRefined(baseCustomer);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseCustomer.getId())) {
			return true;
		}
		BaseCustomer dbItem = selectRefined(baseCustomer);
		if(null == dbItem) {
			return false;
		}
		return !baseCustomer.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return baseCustomerMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public void updateByPrimaryKeySelective(BaseCustomer baseCustomer) {
		baseCustomerMapper.updateByPrimaryKeySelective(baseCustomer);
		
	}
}
